﻿using D.Medicalcare.Domain.Entities;
using D.Medicalcare.ErrorCode;
using D.Medicalcare.Infrastructure.Interfaces;
using D.Medicalcare.Write.API.Application.Commands.MenuCommand;
using MediatR;

namespace D.Medicalcare.Write.API.Application.Handlers.MenuHandler
{
    public class DelRoleMenuHandler : IRequestHandler<DelRoleMenuCommand, APIResult<int>>
    {
        private readonly IBaseRepository<RoleMenu> rmRep;

        public DelRoleMenuHandler(IBaseRepository<RoleMenu> rmRep)
        {
            this.rmRep = rmRep;
        }
        public async Task<APIResult<int>> Handle(DelRoleMenuCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var list = rmRep.GetAll().Where(x => x.RoleId == request.RoleId).ToList();
            if (list == null)
            {
                result.Code = APIEnum.Failed;
                result.Msg = "删除失败";
            }
            var a = await rmRep.DelRange(list);
            result.Code = APIEnum.Success;
            result.Msg = "删除成功";
            result.Data = a;
            return await Task.FromResult(result);

        }
    }
}
